package com.foreveross.fetl.core.domain.task.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.foreveross.fetl.common.jpa.EntityHibernateRepository;
import com.foreveross.fetl.core.domain.task.TaskTemplateEntity;
import com.foreveross.fetl.core.domain.task.repository.ITaskTemplateRepository;
import com.foreveross.fetl.core.domain.task.repository.TaskTemplateEntityRepository;
import com.foreveross.fetl.util.DatabaseBeanUtils;

@Repository("taskTemplateEntityRepository")
public class TaskTemplateEntityRepositoryImpl extends EntityHibernateRepository
		implements TaskTemplateEntityRepository,ITaskTemplateRepository {

	public List<TaskTemplateEntity> findAllDistinctLine(){
		String sql = "select distinct t.from_city as fromCity ,t.to_city as toCity from crawl_task_template t";
		//return this.find(QuerySettings.create(TaskTemplateEntity.class));
		return DatabaseBeanUtils.getBeanList(sql, TaskTemplateEntity.class);
	}
	
	public List<TaskTemplateEntity>  findGralChannelAirLine(String grabChannel) {
		String sql = "select distinct t.from_city as fromCity ,t.to_city as toCity  from crawl_task_template t WHERE t.ATTACH_HBASE_KEY = ?";
		//return this.find(QuerySettings.create(TaskTemplateEntity.class).eq("attachHbaseKey", grabChannel));
		return DatabaseBeanUtils.getBeanList(sql, TaskTemplateEntity.class,new Object[]{grabChannel});
	}

	@Override
	public List<TaskTemplateEntity> findByCondition(String channel,
			String fromCity, String toCity) {
		StringBuffer jpql =new StringBuffer("select tte from TaskTemplateEntity tte where 1=1");
		List<Object> conditionVals = new ArrayList<Object>();
		if(channel!=null){
			jpql.append(" and tte.attachHbaseKey =?");
			conditionVals.add(channel);
		}
		if(fromCity!=null){
			jpql.append(" and tte.fromCity =?");
			conditionVals.add(fromCity);
		}
		if(toCity!=null){
			jpql.append(" and tte.toCity =?");
			conditionVals.add(toCity);
		}
		return super.queryResult(jpql.toString(), conditionVals.toArray());
	}
}
